{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Isomorphic Strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: isIsomorphic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #同构字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定两个字符串&nbsp;<code>s</code>&nbsp;和&nbsp;<code>t</code>&nbsp;，判断它们是否是同构的。</p>\n",
    "\n",
    "<p>如果&nbsp;<code>s</code>&nbsp;中的字符可以按某种映射关系替换得到&nbsp;<code>t</code>&nbsp;，那么这两个字符串是同构的。</p>\n",
    "\n",
    "<p>每个出现的字符都应当映射到另一个字符，同时不改变字符的顺序。不同字符不能映射到同一个字符上，相同字符只能映射到同一个字符上，字符可以映射到自己本身。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = <code>\"egg\"</code>, t = <code>\"add\"</code>\n",
    "<strong>输出：</strong>true\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = <code>\"foo\"</code>, t = <code>\"bar\"</code>\n",
    "<strong>输出：</strong>false</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = <code>\"paper\"</code>, t = <code>\"title\"</code>\n",
    "<strong>输出：</strong>true</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" /></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>\n",
    "\t<li><code>t.length == s.length</code></li>\n",
    "\t<li><code>s</code>&nbsp;和&nbsp;<code>t</code>&nbsp;由任意有效的 ASCII 字符组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [isomorphic-strings](https://leetcode.cn/problems/isomorphic-strings/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [isomorphic-strings](https://leetcode.cn/problems/isomorphic-strings/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"egg\"\\n\"add\"', '\"foo\"\\n\"bar\"', '\"paper\"\\n\"title\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        mapping = dict()\n",
    "        for ch1,ch2 in zip(s,t):\n",
    "            if ch1 in mapping.keys():\n",
    "                if ch2 == mapping[ch1]:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "            else:\n",
    "                if ch2 not in mapping.values():\n",
    "                    mapping[ch1] = ch2\n",
    "                else:\n",
    "                    return False\n",
    "        return True\n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        # hashs={}\n",
    "        # hasht={}\n",
    "        # indexs=0\n",
    "        # indext=0\n",
    "        # for i in s:\n",
    "        #     if i in hashs: \n",
    "        #         # hashs[i].append(indexs)\n",
    "        #         hashs[i]=str(hashs[i])+\",\"+str(indexs)\n",
    "        #     else:\n",
    "        #         # hashs[i]=[s.index(i)]\n",
    "        #         hashs[i]=str(s.index(i))\n",
    "        #     indexs=indexs+1\n",
    "        # for j in t:\n",
    "        #     if j in hasht: \n",
    "        #         hasht[j]=str(hasht[j])+\",\"+str(indext)\n",
    "        #     else:\n",
    "        #         hasht[j]=str(t.index(j))\n",
    "        #     indext=indext+1\n",
    "        # maps={1:2,2:3}\n",
    "        # if list(hasht.values())== list(hashs.values()):\n",
    "        #      return True\n",
    "        # else:\n",
    "        #     return False\n",
    "        char_map = {}\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] in char_map:\n",
    "                if char_map[s[i]] != t[i]: \n",
    "                    return False\n",
    "            elif t[i] in char_map.values(): \n",
    "                return False\n",
    "            else: \n",
    "                char_map[s[i]] = t[i]\n",
    "        print(char_map)\n",
    "        return True\n",
    "        \n",
    "        \n",
    "        \n",
    "            \n",
    "            \n",
    "            \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: 'str', t: 'str') -> 'bool':\n",
    "        mapping = {}\n",
    "        usedKeys = set()\n",
    "        n = len(s)\n",
    "        for i in range(n):\n",
    "            if s[i] in mapping.keys():\n",
    "                if t[i] != mapping[s[i]]:\n",
    "                    return False\n",
    "            else:\n",
    "                if t[i] in mapping.values():\n",
    "                    return False\n",
    "                else:\n",
    "                    mapping[s[i]] = t[i]\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: 'str', t: 'str') -> 'bool':\n",
    "        dic = {}\n",
    "        s = list(s)\n",
    "        for i,n in enumerate(t):\n",
    "            if dic.get(s[i], 0) == 0 and n not in dic.values():\n",
    "                dic[s[i]] = n    \n",
    "            elif dic.get(s[i], 0) == n:\n",
    "                s[i] = n\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: 'str', t: 'str') -> 'bool':\n",
    "        dic = {}\n",
    "        s = list(s)\n",
    "        for i,n in enumerate(t):\n",
    "            if dic.get(s[i], 0) == 0 and n not in dic.values():\n",
    "                dic[s[i]] = n    \n",
    "            elif dic.get(s[i], 0) == n:\n",
    "                s[i] = n\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        hashs={}\n",
    "        hasht={}\n",
    "        indexs=0\n",
    "        indext=0\n",
    "        for i in s:\n",
    "            if i in hashs: \n",
    "                # hashs[i].append(indexs)\n",
    "                hashs[i]=str(hashs[i])+\",\"+str(indexs)\n",
    "            else:\n",
    "                # hashs[i]=[s.index(i)]\n",
    "                hashs[i]=str(s.index(i))\n",
    "            indexs=indexs+1\n",
    "        for j in t:\n",
    "            if j in hasht: \n",
    "                hasht[j]=str(hasht[j])+\",\"+str(indext)\n",
    "            else:\n",
    "                hasht[j]=str(t.index(j))\n",
    "            indext=indext+1\n",
    "        maps={1:2,2:3}\n",
    "        if list(hasht.values())== list(hashs.values()):\n",
    "             return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "        \n",
    "        \n",
    "            \n",
    "            \n",
    "            \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return [s.find(i) for i in s] == [t.find(j) for j in t]\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        tempDics = {}\n",
    "        tempDict = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in tempDics:\n",
    "                tempDics[s[i]] += str(i)\n",
    "            else:\n",
    "                tempDics[s[i]] = str(i)\n",
    "            if t[i] in tempDict:\n",
    "                tempDict[t[i]] += str(i)\n",
    "            else:\n",
    "                tempDict[t[i]] = str(i)\n",
    "        for i in range(len(s)):\n",
    "            if tempDics[s[i]] != tempDict[t[i]]:\n",
    "                return False\n",
    "        return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        k = [0]*len(s)\n",
    "        s = list(s)\n",
    "        t = list(t)\n",
    "        change = list()\n",
    "        for i in range(len(s)):\n",
    "            if k[i]==0 and t[i] not in change:\n",
    "                j=i\n",
    "                tmp = s[i]\n",
    "                while j<len(s):\n",
    "                    if s[j]==tmp and k[j]==0:\n",
    "                        s[j]=t[i]\n",
    "                        k[j]=1\n",
    "                    j=j+1\n",
    "                change.append(t[i])\n",
    "        return s==t\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(set(s)) != len(set(t)):\n",
    "            return False\n",
    "        s = list(s)\n",
    "        s1 = list(s)\n",
    "        t = list(t)\n",
    "        flag = [0 for _ in s]\n",
    "        for idx,i in enumerate(set(t)):\n",
    "            idx = t.index(i)\n",
    "            for idx1 in range(idx,len(s1)):\n",
    "                if flag[idx1] == 0 and s1[idx1] == s[idx] and s1[idx1]!= i:\n",
    "                    s1[idx1] = i\n",
    "                    flag[idx1]=1\n",
    "        return s1==t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(set(s)) != len(set(t)):\n",
    "            return False\n",
    "        s = list(s)\n",
    "        s1 = list(s)\n",
    "        t = list(t)\n",
    "        flag = [0 for _ in s]\n",
    "        for i in set(t):\n",
    "            idx = t.index(i)\n",
    "            for idx1 in range(idx,len(s1)):\n",
    "                if flag[idx1] == 0 and s1[idx1] == s[idx] and s1[idx1]!= i:\n",
    "                    s1[idx1] = i\n",
    "                    flag[idx1]=1\n",
    "        return s1==t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        lens = s.__len__()\n",
    "        lent = t.__len__()\n",
    "        if lens != lent:\n",
    "            return False\n",
    "        ls = [0]*lens\n",
    "        lt = [0]*lent\n",
    "        for i in range(lens):\n",
    "            if i == s.find(s[i]):\n",
    "                ls[i] = i\n",
    "            else:\n",
    "                ls[i] = ls[s.find(s[i])]\n",
    "        for i in range(lent):\n",
    "            if i == t.find(t[i]):\n",
    "                lt[i] = i\n",
    "            else:\n",
    "                lt[i] = lt[t.find(t[i])]\n",
    "        if(lt == ls):\n",
    "            return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        if len(set(s)) != len(set(t)):\n",
    "            return False\n",
    "        \n",
    "        def findplace(string, char):\n",
    "            return (i for i in range(len(string)) if string[i] == char)\n",
    "        \n",
    "        smap,tmap={},{}\n",
    "        for i in range(len(s)):\n",
    "            if smap.get(s[i],None) is None:\n",
    "                smap[s[i]]=[i]\n",
    "            else:\n",
    "                smap[s[i]].append(i)\n",
    "                \n",
    "            if tmap.get(t[i],None) is None:\n",
    "                tmap[t[i]]=[i]\n",
    "            else:\n",
    "                tmap[t[i]].append(i)\n",
    "                \n",
    "        \n",
    "        for i, j in zip(s, t):\n",
    "            if smap[i]!=tmap[j]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        ds, dt = {}, {}\n",
    "        lens = len(s)\n",
    "        i = 0\n",
    "        while i < lens:\n",
    "            l1, l2 = [], []\n",
    "            if s[i] in ds:\n",
    "                ds[s[i]].append(i)\n",
    "                l1 = ds[s[i]]\n",
    "            else:\n",
    "                l1.append(i)\n",
    "                ds[s[i]] = l1\n",
    "            if t[i] in dt:\n",
    "                l2 = dt[t[i]]\n",
    "                dt[t[i]].append(i)\n",
    "            else:\n",
    "                l2.append(i)\n",
    "                dt[t[i]] = l2\n",
    "            if l1 != l2:\n",
    "                return False\n",
    "            i += 1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        n=len(s)\n",
    "        ids,idt={},{}\n",
    "        for i in range(n):\n",
    "            if s[i] in ids:\n",
    "                ids[s[i]].append(i)\n",
    "            else:\n",
    "                ids[s[i]]=[i]\n",
    "            if t[i] in idt:\n",
    "                idt[t[i]].append(i)\n",
    "            else:\n",
    "                idt[t[i]]=[i]\n",
    "            if ids[s[i]]!=idt[t[i]]:\n",
    "                return False\n",
    "        \n",
    "            \n",
    "        return True\n",
    "        \n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        n=len(s)\n",
    "        ids,idt={},{}\n",
    "        for i in range(n):\n",
    "            if s[i] in ids:\n",
    "                ids[s[i]].append(i)\n",
    "            else:\n",
    "                ids[s[i]]=[i]\n",
    "            if t[i] in idt:\n",
    "                idt[t[i]].append(i)\n",
    "            else:\n",
    "                idt[t[i]]=[i]\n",
    "            if ids[s[i]]!=idt[t[i]]:\n",
    "                return False\n",
    "        \n",
    "            \n",
    "        return True\n",
    "        \n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        L = len(s)\n",
    "        ord = [i for i in range(1, L + 1)]\n",
    "        dic_s = dict(zip([i for i in s], ord))\n",
    "        dic_t = dict(zip([i for i in t], ord))\n",
    "        sort_s = [dic_s.get(i) for i in s]\n",
    "        sort_t = [dic_t.get(i) for i in t]\n",
    "        return True if sort_s == sort_t else False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(set(list(zip(s,t)))) == len(set(s)) == len(set(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(set(list(zip(s,t)))) == len(set(s)) == len(set(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: 'str', t: 'str') -> 'bool':\n",
    "        d1, d2 = {}, {}\n",
    "        for i, val in enumerate(s):\n",
    "            d1[val] = d1.get(val, []) + [i]\n",
    "        for i, val in enumerate(t):\n",
    "            d2[val] = d2.get(val, []) + [i]\n",
    "        return sorted(d1.values()) == sorted(d2.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s, t):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :type t: str\n",
    "        :rtype: bool\n",
    "        \"\"\"\n",
    "        return len(set(list(zip(s,t)))) == len(set(s)) == len(set(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        return len(set(s))==len(set(t))==len(set(zip(s, t)))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        d = {}\n",
    "        temp = ''\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d.keys():\n",
    "                d[s[i]] = t[i]\n",
    "            temp += d[s[i]]\n",
    "        l = []\n",
    "        for v in d.values():\n",
    "            if v in l:\n",
    "                return False\n",
    "            else:\n",
    "                l.append(v)\n",
    "        return temp == t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        d1 = {}\n",
    "        d2 = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d1:\n",
    "                if t[i] in d2:\n",
    "                    if d2[t[i]]!=s[i]:\n",
    "                        return False\n",
    "                d1[s[i]] = t[i]\n",
    "                d2[t[i]] = s[i]\n",
    "            else:\n",
    "                if d1[s[i]]!=t[i]:\n",
    "                    return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "\n",
    "\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "\n",
    "        #用s来构造一个t的model出来与t比较\n",
    "        model = ''\n",
    "        dic = {}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in dic:\n",
    "                dic[s[i]] = t[i]\n",
    "            model += dic[s[i]]\n",
    "        return len(set(dic.values())) == len(dic.values()) and model == t \n",
    "                #必须要对dic.values()判重！！！！\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        s_d = dict()\n",
    "        t_s = set()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in s_d:\n",
    "                if not s_d[s[i]] == t[i]:\n",
    "                    return False\n",
    "            else:\n",
    "                if t[i] in t_s:\n",
    "                    return False\n",
    "                else:\n",
    "                    t_s.add(t[i])\n",
    "                    s_d[s[i]] = t[i]\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        dict={}\n",
    "        for i in range(len(s)):\n",
    "            if s[i] in dict:    #判断该元素是否之前出现过\n",
    "                if dict[s[i]]!=t[i]:    #如果之前出现过，现在对应的值却不一样，返回False\n",
    "                    return False\n",
    "            else:\n",
    "                if t[i] in dict.values():   #判断该元素是否在字典的值里面，如果在里面就说明对应的值不一样\n",
    "                    return False\n",
    "                dict[s[i]]=t[i] #将两个值录入字典中(记录下s[i]所变换后的字母)\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        d = dict()\n",
    "        se = \"\"\n",
    "        for c1, c2 in zip(s, t):\n",
    "            if c1 in d:\n",
    "                if c2 == d[c1]:\n",
    "                    pass\n",
    "                else:\n",
    "                    return False\n",
    "            elif c2 not in se:\n",
    "                se += c2\n",
    "                d[c1] = c2\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        temp = {}\n",
    "        if len(s) != len(t):\n",
    "            return False\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in temp:\n",
    "                if t[i] in temp.values():\n",
    "                    return False\n",
    "                temp[s[i]] = t[i]\n",
    "            elif temp[s[i]] != t[i]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        return [*map(s.index, s)] == [*map(t.index, t)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        return [s.index(i) for i in s]==[t.index(i) for i in t]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "       return len(set(s)) == len(set(t))  and len(set(s)) == len(set(zip(s,t)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        for i in range(len(s)):\n",
    "            if s.index(s[i]) == t.index(t[i]):\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        visited_s = defaultdict(int)\n",
    "        visited_t = defaultdict(int)\n",
    "        for i in range(len(s)):\n",
    "            if (s[i] in visited_s) ^ (t[i]  in visited_t):\n",
    "                return False\n",
    "            if s[i] not in visited_s and t[i] not in visited_t:\n",
    "                visited_s[s[i]] = i\n",
    "                visited_t[t[i]] = i\n",
    "            else:\n",
    "                if visited_s[s[i]]!= visited_t[t[i]]:\n",
    "                    return False\n",
    "        return True\n",
    "        \n",
    "\n",
    "            \n",
    "\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        s2t, t2s = {}, {}\n",
    "        length = len(s)\n",
    "        for i in range(length):\n",
    "            c_s, c_t = s[i], t[i]\n",
    "            if (c_s in s2t and s2t[c_s] != c_t) or (c_t in t2s and t2s[c_t] != c_s):\n",
    "                return False\n",
    "            s2t[c_s] = c_t\n",
    "            t2s[c_t] = c_s\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        if len(s)!=len(t):\n",
    "            return False\n",
    "        ss=set()\n",
    "        dd={}\n",
    "        for i in range(len(s)):\n",
    "            if dd.get(s[i])!=None:\n",
    "                if dd[s[i]]!=t[i]:\n",
    "                    return False\n",
    "            else:\n",
    "                if t[i] in ss:\n",
    "                    return False\n",
    "                dd[s[i]]=t[i]\n",
    "                ss.add(t[i])\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        key=list(s)\n",
    "        value=list(t)\n",
    "        d=dict()\n",
    "        t=dict()\n",
    "        for k, v in zip(key, value):\n",
    "            if k not in d:\n",
    "                d.update({k:v})\n",
    "            else:\n",
    "                if v!=d[k]:\n",
    "                    return False\n",
    "            if v not in t:\n",
    "                t.update({v:k})\n",
    "            else:\n",
    "                if k!=t[v]:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        d1, d2 = {}, {}\n",
    "        for c1, c2 in zip(s, t):\n",
    "            #print(c1, c2)\n",
    "            if c1 in d1 and d1[c1] != c2: return False\n",
    "            else: d1[c1] = c2\n",
    "            if c2 in d2 and d2[c2] != c1: return False\n",
    "            else: d2[c2] = c1 \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        s2t,t2s = {},{}\n",
    "        for a,b in zip(s,t): #zip是把s和t一一对应的组成元组对\n",
    "            if a in s2t and s2t[a] != b or b in t2s and t2s[b] != a: #字典里存的是映射关系，注意这一行是键和值分别互换\n",
    "                return False\n",
    "            s2t[a],t2s[b] = b,a\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        dic1 = {}\n",
    "        dic2 = {}\n",
    "        for c1, c2 in zip(s, t):\n",
    "            if c1 in dic1 and dic1[c1] != c2:\n",
    "                return False\n",
    "            else:\n",
    "                dic1[c1] = c2\n",
    "            if c2 in dic2 and dic2[c2] != c1:\n",
    "                return False\n",
    "            else:\n",
    "                dic2[c2] = c1\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def isIsomorphic(self, s, t):\n",
    "        d1, d2 = {}, {}\n",
    "        for c1, c2 in zip(s, t):\n",
    "            if c1 in d1 and d1[c1] != c2: return False\n",
    "            else: d1[c1] = c2\n",
    "            if c2 in d2 and d2[c2] != c1: return False\n",
    "            else: d2[c2] = c1 \n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        differ = Counter()\n",
    "        differ_re = Counter()\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in differ:\n",
    "                if t[i] in differ_re:\n",
    "                    return False\n",
    "                differ[s[i]] = t[i]\n",
    "                differ_re[t[i]] = s[i]\n",
    "            elif differ[s[i]] != t[i] :\n",
    "                return False\n",
    "        print(differ)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def isIsomorphic(self, s: str, t: str) -> bool:\n",
    "        return [*map(s.index, s)] == [*map(t.index, t)]"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
